class ClassesController < ApplicationController
  before_filter :only_teachers,
                :except => [:index, :old, :show, :join, :confirm_join]
  before_filter :only_students,
                :only => [:join, :confirm_join]
  before_filter :get_project
  before_filter :get_class
  
  # Shows active classes list
  #
  def index
    @teachings = @project.active_teachings
    
    respond_to do |wants|
      wants.html
      wants.xml   { render :xml => @teachings.to_xml }
    end
  end
  
  # Shows old classes list
  #
  def old
    @teachings = @project.old_teachings
    
    respond_to do |wants|
      wants.html
      wants.xml   { render :xml => @teaching.to_xml }
    end
  end
  
  # Shows a single class
  #
  def show
    respond_to do |wants|
      wants.html
      wants.xml { render :xml => @teaching.to_xml }
    end
  end
  
  # Shows form to edit a class. Only for GET requests.
  #
  def edit
    
  end
  
  # Updates class
  #
  def update
    if @teaching.update_attributes(params[:teaching])
      respond_to do |wants|
        wants.html  { redirect_to class_url(@project, @teaching) }
        wants.js
        wants.xml   { head :ok }
      end
    else
      respond_to do |wants|
        wants.html do
          flash.now[:error] = 'Unable to save class'
          render :action => "edit"
        end
        wants.js    { render :nothing => true, :status => 500 }
        wants.xml   { render :xml => @teaching.errors.to_xml }
      end
    end
  end
  
  # Shows form to create a new class. Only for GET requests.
  #
  def new
    @teaching = Teaching.new
  end
  
  # Creates new class
  #
  def create
    @teaching = @project.teachings.build(params[:teaching])
    @teaching.user = @logged_user
    
    if @teaching.save
      respond_to do |wants|
        wants.html  { redirect_to class_url(@project, @teaching) }
        wants.xml   { head :created, :location => class_url(@project, @teaching) }
      end
    else
      respond_to do |wants|
        wants.html do
          flash.now[:error] = 'Unable to create class'
          render :action => "new"
        end
        wants.js    { render :nothing => true, :status => 500 }
        wants.xml   { render :xml => @teaching.errors.to_xml }
      end
    end
  end
  
  # Deletes class
  #
  def destroy
    @teaching.destroy
    
    respond_to do |wants|
      wants.html do
        flash[:note] = 'Class deleted'
        redirect_to classes_url(@project)
      end
      wants.js  { head :ok }
      wants.xml { head :ok }
    end
  end
  
  # Displays the "Join class" form
  def join
    unless @project.can_join?(@logged_user)
      respond_to do |wants|
        wants.html  { render :text => "You're already following this project", :layout => true }
        wants.xml   { head :forbidden }
      end
      return
    end
  end
  
  # Lets user join a class
  #
  def confirm_join
    unless @project.can_join?(@logged_user)
      respond_to do |wants|
        wants.html  { render :text => "You're already following this project", :layout => true }
        wants.xml   { head :forbidden }
      end
      return
    end
    unless params[:new_group].blank? # User decided to create a new group
      @group = @teaching.groups.create(:name => params[:new_group], :owner_id => @logged_user.id)
    else
      @group = @teaching.groups.find(:first, :conditions => {:id => params[:group]})
    end
    
    if @group
      @group.users << @logged_user unless params[:new_group].blank? && @group.is_member?(@logged_user.id)
      SimpleMailer.deliver_new_user(@group, @logged_user, @teaching, request.host_with_port) if @group.owner_id != @logged_user.id
      respond_to do |wants|
        wants.html  { redirect_to group_url(@project, @teaching, @group) }
        wants.xml   { head :created, :location => group_url(@project, @teaching, @group) }
      end
    else
      respond_to do |wants|
        wants.html do
          flash.now[:error] = "Unable to join class"
          render :action => "join"
        end
        wants.xml do
          unless params[:new_group].blank? # Couldn't create group
            render :xml => @group.errors.to_xml, :status => 500
          else # Couldn't find group
            head :not_found
          end
        end
      end
    end
  end
private
  
  # If params[:id] is set, retrieves @class
  #
  def get_class
    unless params[:id].nil? || params[:id].blank?
      @teaching = @project.teachings.find(params[:id])
      raise if @teaching.nil?
    end
    return true
  rescue
    respond_to do |wants|
      wants.html { render :text => "The class you're looking for doesn't exist!", :layout => true, :status => 404 }
      wants.js   { render :nothing => true, :status => 404 }
      wants.xml  { head :not_found }
    end
    return false
  end
end
